/*
 * =====================================================================================
 *
 *       Filename:  projection.cpp
 *
 *    Description:  Returns x-projected and y-projected histograms
 *
 *        Version:  1.0
 *        Created:  05/07/12 10:08:22
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Moos Hueting (mh), mooshueting@gmail.com
 *   Organization:  
 *
 * =====================================================================================
 */
#include <iostream>
#include <cv.h>
#include <highgui.h>

using namespace std;
using namespace cv;

int main(int argc, char* argv[]) {
    Mat image;

    image = imread( argv[1], CV_LOAD_IMAGE_GRAYSCALE );
    threshold(image, image, 100, 255, THRESH_BINARY);

    Size s = image.size();

    int cols[s.width];
    int rows[s.height];

    // row projection
    for (int i=0; i < s.height; i++) {
        rows[i] = 0;
        for (int j=0; j < s.width; j++) {
            if (image.at<unsigned char>(i, j) != 0)
                rows[i]++;
        }
    }

    // column projection
    for (int i=0; i < s.width; i++) {
        cols[i] = 0;
        for (int j=0; j < s.height; j++) {
            if (image.at<unsigned char>(j, i) != 0)
                cols[i]++;
        }
    }

    for (int i=0; i < s.height - 1; i++)
        cout << rows[i] << ", ";
    cout << rows[s.height - 1] << endl;

    for (int i=0; i < s.width- 1; i++)
        cout << cols[i] << ", ";
    cout << cols[s.width - 1] << endl;

}
